#include <QStyleOption>
#include <QPainter>
#include "twtankerwidget.h"
#include "ui_twtankerwidget.h"

TwTankerWidget::TwTankerWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TwTankerWidget)
{
    ui->setupUi(this);

    initGunGroup();

    for(int i=0;i<16; i++)
    {
        setGunVisable(i+1, false);
    }
}

TwTankerWidget::~TwTankerWidget()
{
    delete ui;
}

void TwTankerWidget::setTankerNo(int iTankerNo)
{
    m_iTankerNo = iTankerNo;
    ui->label_iIdx->setText(QString("%1#").arg(iTankerNo));
}

int TwTankerWidget::getTankerNo() const
{
    return m_iTankerNo;
}

void TwTankerWidget::addGun(int index, int iGunIdx, int iState)
{
    setGunVisable(index, true);
    setGunTitle(index, iGunIdx);
    setOilState(index, iState);
}

void TwTankerWidget::setOilState(int index, int iState)
{
    switch (iState) {
    case GUN_OFFLINE:
        mapOilState[index]->setStyleSheet("border-image: url(:/MyParts/images/GunOffline.png);");
        break;
    case GUN_ONLINE:
        mapOilState[index]->setStyleSheet("border-image: url(:/MyParts/images/GunNormal.png);");
        break;
    case GUN_FUELING:
        mapOilState[index]->setStyleSheet("border-image: url(:/MyParts/images/GunFuel.png);");
        break;
    case GUN_PREWARNING:
        mapOilState[index]->setStyleSheet("border-image: url(:/MyParts/images/GunPreWarning.png);");
        break;
    case GUN_WARNING:
        mapOilState[index]->setStyleSheet("border-image: url(:/MyParts/images/GunWarning.png);");
        break;
    default:
        mapOilState[index]->setStyleSheet("border-image: url(:/MyParts/images/GunOffline.png);");
        break;
    }
}

void TwTankerWidget::initGunGroup()
{
    for(int i=0; i<16; i++)
    {
        QString name = QString("label%1").arg(i+1);
        QLabel* label = this->findChild<QLabel *>(name);
        mapGunNo[i+1]    = label;

        name = QString("label_Gun%1").arg(i+1);
        label = this->findChild<QLabel*>(name);
        mapOilState[i+1] = label;
    }
}

void TwTankerWidget::setGunVisable(int index, bool bState)
{
    mapGunNo[index]->setVisible(bState);
    if(bState)
    {
        mapOilState[index]->setStyleSheet("border-image: url(:/MyParts/images/GunOffline.png);");
    }
    else
    {
        mapOilState[index]->setStyleSheet("border-image: url(:/MyParts/images/GunHide.png);");
    }

    //mapOilState[index]->setVisible(bState);
}

void TwTankerWidget::setGunTitle(int index, int iGunIdx)
{
    QString str = QString("%1").arg(iGunIdx);
    mapGunNo[index]->setText(str);
}

void TwTankerWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)

    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

